package com.ggh.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ggh.dao.UserMapper;
import com.ggh.entity.User;
import com.ggh.entity.UserBean;
import com.ggh.service.UserService;
import com.ggh.vo.Body;
import com.ggh.vo.Meta;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ： jsq
 * @date ：2019/10/13 22:24
 * @description : 提现
 * @path : com.ggh.service.impl.WithDrawServiceImpl
 * @modifiedBy ：
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
    * @description :根据用户名获取数据
     * 登录使用
    * @param name 
    * @return: com.ggh.entity.User
    * @date : 2019/10/16 13:52
    * @author : jj
    * @throws
    */
    @Override
    public User getUserByName(String name) {
        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("login_name", name));
        return user;
    }
    /**
    * @description :微信登陆
    * @param openId
    * @return: com.ggh.entity.User
    * @date : 2019/10/16 14:33
    * @author : jj
    * @throws
    */
    @Override
    public User getByOpenid(String openId) {
        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("openid", openId));
        return user;
    }

    @Override
    public Body edit(User user) {
        if(user.getUserid()==0){
            return Body.newInstance("用户ID不能为空");
        }
        if(StringUtils.isBlank(user.getLoginName())){
            return Body.newInstance("登录名不能为空");
        }
        int b = baseMapper.updateById(user);
        return b>0? Body.newInstance("操作成功"):Body.newInstance(Meta.CODE_500,"操作失败！");
    }

    @Override
    public Body changePassword(String oldPwd, String newPwd, String confirmPwd ){
        if(StringUtils.isBlank(oldPwd)){
            return Body.newInstance(Meta.CODE_500,"旧密码不能为空");
        }
        if(StringUtils.isBlank(newPwd)){
            return Body.newInstance(Meta.CODE_500,"新密码不能为空");
        }
        if(StringUtils.isBlank(confirmPwd)){
            return Body.newInstance(Meta.CODE_500,"确认密码不能为空");
        }
        if(!confirmPwd.equals(newPwd)){
            return Body.newInstance(Meta.CODE_500,"确认密码与新密码不一致");
        }
        User olduser = (User) SecurityUtils.getSubject().getPrincipal();
        User user =baseMapper.selectById(olduser.getUserid());
        if(!user.getPassword().equals(oldPwd)){
            return Body.newInstance(Meta.CODE_500,"旧密码错误");
        }
        user.setPassword(newPwd);
        int i = baseMapper.updateById(user);
        return i>0? Body.newInstance("操作成功"):Body.newInstance(Meta.CODE_500,"操作失败");
    }

    @Override
    public Body saveUserInfo(Integer userid, String loginName, String phone, String avatarurl, String nickname){
        if(userid==0){
            return Body.newInstance(Meta.CODE_500,"用户ID不能为空");
        }
        if(StringUtils.isBlank(loginName)){
            return  Body.newInstance(Meta.CODE_500,"登录名不能为空");
        }
        User user = baseMapper.selectById(userid);
        user.setLoginName(loginName);
        user.setNickname(nickname);
        user.setPhone(phone);
        user.setAvatarurl(avatarurl);
        boolean b = user.updateById();
        return  b?Body.newInstance("操作成功"):Body.newInstance(Meta.CODE_500,"操作失败");
    }

    @Override
    public UserBean selectByuserId(Integer userid){
        User user = baseMapper.selectById(userid);
        if (user==null){
            return null;
        }
        UserBean userBean=new UserBean();
        BeanUtils.copyProperties(user,userBean);
        return userBean;
    }
}
